#include "window.h"
#include "cpplexer.h"
#include <QTextEdit>
#include <QTextCursor>
#include "parser.h"
#include "interpreter.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>



window::window(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui = new Ui::windowClass;
	ui->setupUi(this);
	this->hightlight = false;
	connect(ui->text ,SIGNAL(cursorPositionChanged ()),this,SLOT(hightlighttext()));
	connect(ui->generateResult ,SIGNAL(clicked ()),this,SLOT(generateresult()));
	connect(ui->hilighting ,SIGNAL(triggered ()),this,SLOT(changeHightlightPolicy()));
	connect(ui->loadFile ,SIGNAL(triggered ()),this,SLOT(openFile()));
}

window::~window()
{

}
 
void window::hightlighttext()
{
	if (this->hightlight)
	{
		int begin =0;
		int end = 1;
		QTextCharFormat block;
		QTextCharFormat predicate;
		QTextCharFormat type;
		QTextCharFormat variable;
		QTextCharFormat value;
		block.setForeground(Qt::darkYellow);
		predicate.setForeground(Qt::black);
		type.setForeground(Qt::darkBlue);
		value.setForeground(Qt::blue);
		variable.setForeground(Qt::darkGreen);
		QTextDocument* text = ui->text->document();
		QTextCursor cursor(text);
		cpplexer lex;
		lex.createBufferFromString(ui->text->document()->toPlainText().toAscii().data());
		int current=0;
		cursor.setPosition(begin, QTextCursor::MoveAnchor);
		cursor.setPosition(begin, QTextCursor::KeepAnchor);
		do
		{
			QTextCharFormat	  temp;
			try{
			current = lex.next();
			}catch(char* string){current=9;}
			switch(current)
			{
			case 1:
				temp=predicate;
				break;
			case 2:
				temp=variable;
				break;
			case 3:
				temp=type;
				break;
			case 4:
				temp=predicate;
				break;
			case 6:
				temp=block;
				break;
			case 8:
				temp=value;
				break;
			case 9:
				temp.setForeground(Qt::black);
				temp.setBackground(Qt::red);
			default:
				temp.setForeground(Qt::black);
			}
			cursor=	text->find(QString(lex.token),cursor);
			cursor.setCharFormat(temp);
		} while(current !=0);
	}
}

void window::generateresult()
{
	cpplexer* lexer = new cpplexer;
	lexer->createBufferFromString(ui->text->document()->toPlainText().toAscii().data());
	bool exception=false;
	interpreter* inter = NULL;
	parser* par = new parser(lexer);
	QString news;
	try{
		par->analyse_blocks();
		inter = new interpreter(par);
	}catch(char * error) {
		news = error;
		exception = true;
	}
	if(!exception) 
	{
		news = inter->get_result();
	}
	QMessageBox msgBox;
	msgBox.setText(news);
	msgBox.setStandardButtons(QMessageBox::Ok);
	msgBox.setDefaultButton(QMessageBox::Ok);
	msgBox.show();
	int ret = msgBox.exec();
	delete lexer;
	delete par;
	if(inter!=NULL) 
	{
		delete inter;
	}
}

void window::changeHightlightPolicy()
{
	this->hightlight = !this->hightlight;
	QCursor cursor = ui->text->cursor();
	QString text = ui->text->document()->toPlainText();
	ui->text->setText(text);
}

void window::openFile()
{
	QString fileName = QFileDialog::getOpenFileName(this,"Open File","","Files (*.*)");
	QFile file(fileName);
	file.open(QIODevice::ReadOnly);
	QString text = file.readAll();
	file.close();
	ui->text->setText(text);
}